package com.eboss2.sbs.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.myapplication.MyApplication;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;
import com.tool.utils.utils.UtilPreference;
import com.tool.utils.view.MyCountDownTimer;
import com.tool.utils.view.ShapeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;

public class ActivityForgotLoginPwd extends BaseActivity implements OnClickListener {

    private final String TAG = "ActivityForgotPayPwd";
    private EditText et_code;
    private EditText et_username;
    private EditText et_phone;
    private TextView tv_getcode;
    private EditText et_new_pwd;
    private EditText et_comfirm_new_pwd;
    private ShapeView btnCommit;

    private String validCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_forgot_login_pwd);
        MyActivityManager.getInstance().addActivity(this);
        showView(R.id.titleback_btn_back, true);
        findViewById(R.id.titleback_btn_back).setOnClickListener(this);
        setTvText(R.id.tv_title, "忘记密码");
        showView(R.id.next_sure, false);
        initView();
    }


    private void initView() {

        tv_getcode = textView(R.id.tv_getcode);
        tv_getcode.setOnClickListener(this);
        et_code = editText(R.id.et_code);
        et_phone = editText(R.id.et_phone);
        et_username = editText(R.id.et_username);
        et_new_pwd = editText(R.id.et_new_pwd);
        et_comfirm_new_pwd = editText(R.id.et_comfirm_new_pwd);

        tv_getcode.setOnClickListener(this);
        btnCommit = findViewById(R.id.btn_commit);
        btnCommit.setOnClickListener(this);
//        button(R.id.btn_commit).setOnClickListener(this);


//        EventBus.getDefault().register(this);



    }


//    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
//    public void getOrderNo(String event){
//        et_phone.setText(event);
//        et_phone.setFocusable(false);
//    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册 , 防止Activity内存泄漏
        EventBus.getDefault().unregister( this ) ;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.titleback_btn_back:
                finish();
                break;
//            case R.id.btn_next:
//                if (StringUtils.isBlank(et_code.getText().toString().trim())) {
//                    ToastUtils.showShort(this, "验证码不能为空");
//                    et_code.requestFocus();
//                    return;
//                }
//                ll_code.setVisibility(View.GONE);
//                ll_edit_pwd.setVisibility(View.VISIBLE);
//                validCode = et_code.getText().toString().trim();
//                break;
            case R.id.btn_commit://提交
                try {
                    doCommit();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_getcode://获取验证码
                if (StringUtils.isBlank(getTvText(R.id.et_phone))){
                    ToastUtils.showShort(this, "手机号不能为空");
                    return;
                }
                getValidCode();// 从服务器获取验证码
                break;
            default:
                break;
        }
    }

    /**
     * 从服务器获取验证码
     *
     */
    private void getValidCode() {
        MyCountDownTimer countDowntimer = new MyCountDownTimer(Config.VOLID_CODE_SECONDS, 1000, tv_getcode,
                getResources().getDrawable(R.drawable.reg_suc_bar1), getResources().getDrawable(R.drawable.reg_suc_bar2));
        countDowntimer.start();

        RequestParams params = new RequestParams();
        params.add("phone",  getTvText(R.id.et_phone));
//        params.add("groupId",  MyApplication.getmInstance().getLoginMEntity().getGroupId()+"");
        params.add("type",  "2");
        showProgress("正在发送");
        HttpUtil.get(Config.SBS_GET_CODE, params, new HttpUtil.RequestListener() {

            @Override
            public void success(String response) {
                disShowProgress();
                try {
                    JSONObject result = new JSONObject(response);
                    if (result.optInt("code") == -2){
                        UtilPreference.clearNotKeyValues(mContext);
                        // 退出账号 返回到登录页面
                        MyActivityManager.getInstance().logout(mContext);
                        return;
                    }
                    if (!result.optBoolean("status")) {
                        ToastUtils.CustomShow(mContext, result.optString("message"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }

        });
    }


    /**
     * 提交
     */
    private void doCommit() throws ParseException {

        if (StringUtils.isBlank(getTvText(R.id.et_username))){
            ToastUtils.showShort(this, "请输入登录账号");
            return;
        }

        if (StringUtils.isBlank(getTvText(R.id.tv_getcode))){
            ToastUtils.showShort(this, "验证码不能为空");
            return;
        }

        if (StringUtils.isBlank(et_new_pwd.getText().toString().trim())) {
            ToastUtils.showShort(this, "新支付密码不能为空");
            et_new_pwd.requestFocus();
            return;
        }

        if (StringUtils.isBlank(et_comfirm_new_pwd.getText().toString().trim())) {
            ToastUtils.showShort(this, "确认密码不能为空");
            et_comfirm_new_pwd.requestFocus();
            return;
        }

        if (!StringUtils.isEquals(et_new_pwd.getText().toString().trim(), et_comfirm_new_pwd.getText().toString().trim())) {
            ToastUtils.showShort(this, "两次输入密码不一致");
            return;
        }



        showProgress("加载中...");
        RequestParams params = new RequestParams();
//        params.add("userId", MyApplication.getmInstance().getLoginMEntity().getId()+"");
        params.add("username", getTvText(R.id.et_username));
        params.add("phone", getTvText(R.id.et_phone));
        params.add("password", et_new_pwd.getText().toString().trim());
        params.add("code", getTvText(R.id.et_code));
        params.add("type", "2");
        HttpUtil.get(Config.SBS_FORGOT_PWD, params, requestListener);

    }

    private HttpUtil.RequestListener requestListener = new HttpUtil.RequestListener() {

        @Override
        public void success(String response) {
            disShowProgress();
            try {
                JSONObject result = new JSONObject(response);
                if (result.optInt("code") == -2){
                    UtilPreference.clearNotKeyValues(mContext);
                    // 退出账号 返回到登录页面
                    MyActivityManager.getInstance().logout(mContext);
                    return;
                }
                if (!result.optBoolean("status")) {
                    ToastUtils.CustomShow(mContext, result.optString("message"));
                }else{
                    ToastUtils.CustomShow(mContext, result.optString("message"));
                    UtilPreference.clearNotKeyValues(mContext);
                    // 退出账号 返回到登录页面
                    MyActivityManager.getInstance().logout(mContext);
                }



            } catch (Exception e) {
                Log.e(TAG, "doCommit() Exception: " + e.getMessage());
            }
        }

        @Override
        public void failed(Throwable error) {
            disShowProgress();
            showE404();
        }
    };


}
